<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
       <el-form-item label="渠道名称" prop="channelName">
        <el-input v-model="dataForm.channelName" placeholder="渠道名称"></el-input>
      </el-form-item>
      <el-form-item label="渠道ID" prop="channelId">
        <el-input v-model="dataForm.channelId" placeholder="渠道ID"></el-input>
      </el-form-item>
      <el-form-item label="渠道商户ID" prop="channelMchId">
        <el-input v-model="dataForm.channelMchId" placeholder="渠道商户ID"></el-input>
      </el-form-item>
      <el-form-item label="商户ID" prop="mchId">
        <el-input v-model="dataForm.mchId" placeholder="商户ID"></el-input>
      </el-form-item>
      <el-form-item label="配置参数" prop="param">
        <el-input type = "textarea" v-model="dataForm.param" placeholder="配置参数"></el-input>
      </el-form-item>
       <el-form-item label="备注" prop="remark">
        <el-input type = "textarea" v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
      <el-form-item label="状态" size="mini" prop="state">
        <el-radio-group v-model="dataForm.state">
          <el-radio :label="1">禁用</el-radio>
          <el-radio :label="0">正常</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { isEmail, isMobile } from '@/utils/validate'
  export default {
    data () {
      var validatePassword = (rule, value, callback) => {
        if (!this.dataForm.id && !/\S/.test(value)) {
          callback(new Error('密码不能为空'))
        } else {
          callback()
        }
      }
      var validateComfirmPassword = (rule, value, callback) => {
        if (!this.dataForm.id && !/\S/.test(value)) {
          callback(new Error('确认密码不能为空'))
        } else if (this.dataForm.password !== value) {
          callback(new Error('确认密码与密码输入不一致'))
        } else {
          callback()
        }
      }
      var validateEmail = (rule, value, callback) => {
        if (!isEmail(value)) {
          callback(new Error('邮箱格式错误'))
        } else {
          callback()
        }
      }
      var validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        roleList: [],
        dataForm: {
          id: 0,
          channelId: '',
          channelName: '',
          channelMchId: '',
          mchId: '',
          state: 0,
          param: '',
          remark: '',
          createTime: ''
        },
        dataRule: {
          userName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          password: [
            { validator: validatePassword, trigger: 'blur' }
          ],
          comfirmPassword: [
            { validator: validateComfirmPassword, trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' },
            { validator: validateEmail, trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' },
            { validator: validateMobile, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.$http({
          url: this.$http.adornUrl('/sys/mchChannel/list'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.roleList = data
        }).then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs.dataForm.resetFields()
          })
        }).then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/mchChannel/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              this.dataForm.channelId = data.channelId;
              this.dataForm.channelName = data.channelName;
              this.dataForm.channelMchId = data.channelMchId;
              this.dataForm.mchId = data.mchId;
              this.dataForm.param = data.param;
              this.dataForm.remark = data.remark;
              this.dataForm.createTime = data.createTime;
              this.dataForm.state = data.state;
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/mchChannel`),
              method: this.dataForm.id ? 'put' : 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'channelId': this.dataForm.channelId,
                'channelName': this.dataForm.channelName,
                'channelMchId': this.dataForm.channelMchId,
                'mchId': this.dataForm.mchId,
                'state': this.dataForm.state,
                'param': this.dataForm.param,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        })
      }
    }
  }
</script>
